package com.prs.crm.service.sys.impl;

import java.util.Collection;
import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.dao.sys.ShortCutMenuDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.sys.ShortCutMenu;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.sys.MenuService;
import com.prs.crm.service.sys.ShortCutMenuService;

public class ShortCutMenuServiceImpl extends BaseServiceImpl<ShortCutMenu,ShortCutMenuDao> implements ShortCutMenuService{

	@Autowired
	private MenuService menuService;
	
	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public Collection<ShortCutMenu> findMenus(Person person)
	{
		return this.getDao().findMenus(person);
	}
	
	public void saveShortCutMenus(Person person,Integer[] menuIds)
	{
		if (menuIds!=null && menuIds.length>0)
		{
			Collection<ShortCutMenu> menus = this.findMenus(person);
			Collection<Integer> ids = new HashSet<Integer>();
			for (Integer menuid : menuIds)
			{
				ids.add(menuid);
			}
			for (ShortCutMenu shortcutmenu : menus)
			{
				if (!ids.contains(shortcutmenu.getMenu().getId()))
				{
					this.getDao().remove(shortcutmenu);
				}
				else
				{
					ids.remove(shortcutmenu.getMenu().getId());
				}
			}
			for (Integer menuid : ids)
			{
				ShortCutMenu newmenu = new ShortCutMenu();
				newmenu.setMenu(this.getMenuService().get(menuid));
				this.save(newmenu);
			}
		}
		else
		{
			this.getDao().removeMenus(person);
		}
		
	}
}
